<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <meta name="layout" content="main"/>
  <title>Edit Event</title>
</head>
<body>
<div class="nav">
  <span class="menuButton"><a class="home" href="${createLinkTo(dir: '')}">Home</a></span>
  <g:render template="/common/menubar"/>
</div>
<div class="body">
  <h1>Edit Event</h1>
  <g:if test="${flash.message}">
    <div class="message">${flash.message}</div>
  </g:if>
  <g:hasErrors bean="${eventInstance}">
    <div class="errors">
      <g:renderErrors bean="${eventInstance}" as="list"/>
    </div>
  </g:hasErrors>
  <g:form method="post">
    <input type="hidden" name="id" value="${eventInstance?.id}"/>
    <input type="hidden" name="version" value="${eventInstance?.version}"/>
    <div class="dialog">
      <table>
        <tbody>

        <tr class="prop">
          <td valign="top" class="name">
            <label for="name">Name:</label>
          </td>
          <td valign="top" class="value ${hasErrors(bean: eventInstance, field: 'name', 'errors')}">
            <input type="text" maxlength="40" id="name" name="name" value="${fieldValue(bean: eventInstance, field: 'name')}"/>
          </td>
        </tr>

        <tr class="prop">
          <td valign="top" class="name">
            <label for="description">Description:</label>
          </td>
          <td valign="top" class="value ${hasErrors(bean: eventInstance, field: 'description', 'errors')}">
            <textarea rows="5" cols="40" name="description">${fieldValue(bean: eventInstance, field: 'description')}</textarea>
          </td>
        </tr>

        <tr class="prop">
          <td valign="top" class="name">
            <label for="location">Location:</label>
          </td>
          <td valign="top" class="value ${hasErrors(bean: eventInstance, field: 'location', 'errors')}">
            <input type="text" maxlength="50" id="location" name="location" value="${fieldValue(bean: eventInstance, field: 'location')}"/>
          </td>
        </tr>

        <tr class="prop">
          <td valign="top" class="name">
            <label for="startDate">Start Date:</label>
          </td>
          <td valign="top" class="value ${hasErrors(bean: eventInstance, field: 'startDate', 'errors')}">
            <g:datePicker name="startDate" value="${eventInstance?.startDate}"></g:datePicker>
          </td>
        </tr>

        <tr class="prop">
          <td valign="top" class="name">
            <label for="endDate">End Date:</label>
          </td>
          <td valign="top" class="value ${hasErrors(bean: eventInstance, field: 'endDate', 'errors')}">
            <g:datePicker name="endDate" value="${eventInstance?.endDate}"></g:datePicker>
          </td>
        </tr>

        <tr class="prop">
          <td valign="top" class="name">
            <label for="administrator">Administrator:</label>
          </td>
          <td valign="top" class="value ${hasErrors(bean: eventInstance, field: 'administrator', 'errors')}">
            <g:select optionKey="id" from="${UserAccount.list()}" name="eventId" value="${eventInstance?.id}"></g:select>
          </td>
        </tr>

        %{--<tr class="prop">--}%
        %{--<td valign="top" class="name">--}%
        %{--<label for="positions">Positions:</label>--}%
        %{--</td>--}%
        %{--<td valign="top" class="value ${hasErrors(bean: eventInstance, field: 'positions', 'errors')}">--}%
        %{--<g:select name="positions"--}%
        %{--from="${Position.list()}"--}%
        %{--size="5" multiple="yes" optionKey="id"--}%
        %{--value="${eventInstance?.positions}"/>--}%

        %{--</td>--}%
        %{--</tr>--}%

        </tbody>
      </table>
    </div>
    <div class="buttons">
      <span class="button"><g:actionSubmit class="save" value="Update Event" action="Update"/></span>
      %{--<span class="button"><g:actionSubmit class="delete" onclick="return confirm('Are you sure?');" value="Delete"/></span>--}%
    </div>
  </g:form>
  <h1>Positions</h1>
  <div class="list">
    <g:if test="${flash.messagePosition}">
      <div class="message">${flash.messagePosition}</div>
    </g:if>    
    <g:if test="${flash.messagePositionError}">
      <div class="errors">${flash.messagePositionError}</div>
    </g:if>
    <table>
      <thead>
      <tr>
        %{--<g:sortableColumn property="id" title="Id"/>--}%
        <g:sortableColumn property="name" title="Name"/>
        <g:sortableColumn property="description" title="Description"/>
        <g:sortableColumn property="location" title="Location"/>
        <g:sortableColumn property="requires" title="Requires"/>
        <g:sortableColumn property="available" title="Available"/>
        <g:sortableColumn property="startDate" title="Start Date"/>
        <g:sortableColumn property="ignore" title="Action" />
      </tr>
      </thead>
      <tbody>
      <g:if test="${eventInstance?.positions?.size() == 0}">
        <tr>
          <td colspan=7>No positions are defined</td>
        </tr>
      </g:if>      
      <g:each in="${eventInstance?.positions}" status="i" var="positionInstance">
        <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
          <td><g:link controller="position" action="edit" params="[id:positionInstance.id, eventId:eventInstance.id]">${fieldValue(bean: positionInstance, field: 'name')}</g:link></td>
          %{--<td>${fieldValue(bean: positionInstance, field: 'name')}</td>--}%
          <td>${fieldValue(bean: positionInstance, field: 'description')}</td>
          <td>${fieldValue(bean: positionInstance, field: 'location')}</td>
          <td>${fieldValue(bean: positionInstance, field: 'requires')}</td>
          <td>${positionInstance.requires - positionInstance?.volunteers?.size()}</td>
          <td>${fieldValue(bean: positionInstance, field: 'startDate')}</td>
          <td><span class="button"><g:link class="delete" onclick="return confirm('Are you sure?');" controller="event" action="deletePosition" params="[eventId:eventInstance?.id, positionId:positionInstance.id]">Delete Position</g:link></span></td>
        </tr>
      </g:each>
      </tbody>
    </table>
  </div>
%{--<div class="paginateButtons">--}%
%{--<g:paginate total="${eventInstance.positions.count}" />--}%
%{--</div>--}%
  <g:form method="post" controller="position">
    <input type="hidden" name="eventId" value="${eventInstance?.id}"/>
    <div class="buttons">
      <span class="button"><g:actionSubmit class="add" value="Add Position" action="create" /></span>
      %{--<span class="button"><g:link class="create" controller="position" action="create" params="[eventId:eventInstance.id]">Add Position</g:link></span>--}%
      %{--<span class="button"><g:actionSubmit class="delete" onclick="return confirm('Are you sure?');" value="Delete"/></span>--}%
    </div>
  </g:form>

</div>
</body>
</html>
